package com.demo.commcial.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.commcial.entity.AdminOper;
import com.demo.commcial.mapper.AdminOperMapper;
import com.demo.commcial.service.IAdminOperService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-10-25
 */
@Controller
@RequestMapping("/adminOper")
public class AdminOperController {

    private final Logger logger = LoggerFactory.getLogger(AdminOperController.class);

    @Resource
    AdminOperMapper adminOperMapper;

    @Autowired
    private IAdminOperService adminOperService;

    // 查询所有列
    @RequestMapping("list")
    public String list(Model model, HttpServletRequest request){
        List<AdminOper> adminOper = adminOperService.list();
        String userName = request.getParameter("userName");
        AdminOper ao = adminOperService.findByUsername("admin");
        System.out.println("自定义sql查询：" + ao.getLoginAccount());
        logger.info("自定义sql查询：" + ao.getLoginAccount());
        QueryWrapper<AdminOper> wrapper = new QueryWrapper();
        wrapper.eq("Login_Account","admin");
        AdminOper ao1 = adminOperService.getOne(wrapper);
        System.out.println(ao1.getLoginPassword());
//        System.out.println("HuTool加密" + SecureUtil.md5("123456"));
        model.addAttribute("adminOper",adminOper);

        return "list";
    }

    //添加页面跳转
    @RequestMapping("add")
    private String add(){
        return "add";
    }

    //添加
    @RequestMapping("adduser")
    private String addUsers(Model model,AdminOper adminOper){
        boolean b= adminOperService.save(adminOper);
        model.addAttribute("u",b);
        return "redirect:/adminOper/list";
    }

    //删除
    @RequestMapping("delete/{accountId}")
    private String deleteUsers(@PathVariable("accountId") Integer id){
        adminOperMapper.deleteById(id);
        return "redirect:/adminOper/list";
    }

    //跳转修改页面
    @RequestMapping("update/{accountId}")
    private String updateUsers(@PathVariable("accountId")Integer id,Model model){
        AdminOper adminOper=adminOperMapper.selectById(id);
        model.addAttribute("adminOper",adminOper);
        return "update";
    }

    //修改
    @RequestMapping("update")
    private String update(AdminOper adminOper){
        adminOperService.saveOrUpdate(adminOper);
        return "redirect:/adminOper/list";
    }


}

